package com.parenting.oauth2.config;


import com.parenting.oauth2.service.JwtUser;
import com.parenting.properties.BlogProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 扩展响应的认证信息
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    @Autowired
    private BlogProperties blogProperties;

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken,
                                     OAuth2Authentication oAuth2Authentication) {
        JwtUser user = (JwtUser) oAuth2Authentication.getPrincipal();
        if (StringUtils.isNotEmpty(user.getMobile())){
            String str = "****";
            StringBuilder sb = new StringBuilder(user.getMobile());
            sb.replace(3, 7, str);
            user.setMobile(sb.toString());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", user);
        // 设置附加信息
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(map);

        return oAuth2AccessToken;
    }

}
